/**
  ******************************************************************************
  * File Name          : main.c
  * Description        : Main program body
  ******************************************************************************
  *
  * Copyright (c) 2017 STMicroelectronics International N.V. 
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without 
  * modification, are permitted, provided that the following conditions are met:
  *
  * 1. Redistribution of source code must retain the above copyright notice, 
  *    this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright notice,
  *    this list of conditions and the following disclaimer in the documentation
  *    and/or other materials provided with the distribution.
  * 3. Neither the name of STMicroelectronics nor the names of other 
  *    contributors to this software may be used to endorse or promote products 
  *    derived from this software without specific written permission.
  * 4. This software, including modifications and/or derivative works of this 
  *    software, must execute solely and exclusively on microcontroller or
  *    microprocessor devices manufactured by or for STMicroelectronics.
  * 5. Redistribution and use of this software other than as permitted under 
  *    this license is void and will automatically terminate your rights under 
  *    this license. 
  *
  * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" 
  * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT 
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
  * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT 
  * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f4xx_hal.h"
#include "fatfs.h"
#include "usb_device.h"

/* USER CODE BEGIN Includes */

#include "usbd_cdc_if.h" 

#include "ADS1298.h"
#include "MPU9250.h"
#include "BME280.h"
//#include "esp8266.h"
#include "microSD.h"

#include "stdbool.h"


/************************/
// usbd_cdc.h
//#define CDC_DATA_HS_MAX_PACKET_SIZE 64  
/**********************/

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;

SD_HandleTypeDef hsd;

SPI_HandleTypeDef hspi1;
SPI_HandleTypeDef hspi3;

UART_HandleTypeDef huart3;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

bool verbose = false;		// turn on/off Serial feedback

bool startAcq = false;

volatile uint32_t timerPowerDown = 0;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
static void MX_SPI3_Init(void);
static void MX_I2C1_Init(void);
static void MX_SDIO_SD_Init(void);
static void MX_USART3_UART_Init(void);

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

void USB_Print(unsigned char* s);
void USB_SendBits(uint8_t b);
void USB_SendNumber(int32_t num);

void USART_Send(uint8_t* cmd);
void USART_Send2Byte(uint8_t* fb); 
void USART_Send4Byte(uint8_t* fb); 

void USB_SendByte( uint8_t* fb);
void USB_Send2Byte(uint8_t* fb);
void USB_Send3Byte(uint8_t* fb);
void USB_Send4Byte(uint8_t* fb);

uint8_t transferSPI(uint8_t send);
uint8_t transferMPU(uint8_t send);

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */
	
/* USER CODE END 0 */

int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USB_DEVICE_Init();
  MX_SPI1_Init();
  MX_SPI3_Init();
  MX_I2C1_Init();
  MX_SDIO_SD_Init();
  MX_FATFS_Init();
  MX_USART3_UART_Init();

  /* USER CODE BEGIN 2 */
	HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
	
	HAL_Delay(500);
	
	
	ADS_Init();
	MPU_Init();
	BME_Init();

	//SD_Init();

	
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
	//HAL_UART_Receive_IT(&huart3, &ansESP,1);
	
	while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
		
		 // ------- Next for work mode ---------
		if(startAcq){
			startAcq = false;
			
			MPU_ReadAcc(); // Read Ax Ay Az from MPU9250
			BME_readData();
			
			//writeSD();
			
			BME_SendData();
			MPU_SendData();
		}
		
			
		if(intDRDY){
			startAcq = true;
			intDRDY = false;
			//ADS_updateChannelData();
			ADS_RDATA();
			ADS_SendData();
			ADS_START();
		}	
		
  }
  /* USER CODE END 3 */

}

/** System Clock Configuration
*/
void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

    /**Configure the main internal regulator output voltage 
    */
  __HAL_RCC_PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

    /**Initializes the CPU, AHB and APB busses clocks 
    */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 72;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 3;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

    /**Initializes the CPU, AHB and APB busses clocks 
    */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }

    /**Configure the Systick interrupt time 
    */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick 
    */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/* I2C1 init function */
static void MX_I2C1_Init(void)
{

  hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 100000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }

}

/* SDIO init function */
static void MX_SDIO_SD_Init(void)
{

  hsd.Instance = SDIO;
  hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
  hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
  hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
  hsd.Init.BusWide = SDIO_BUS_WIDE_1B;
  hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
  hsd.Init.ClockDiv = 13;

}

/* SPI1 init function */
static void MX_SPI1_Init(void)
{

  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 10;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }

}

/* SPI3 init function */
static void MX_SPI3_Init(void)
{

  hspi3.Instance = SPI3;
  hspi3.Init.Mode = SPI_MODE_MASTER;
  hspi3.Init.Direction = SPI_DIRECTION_2LINES;
  hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi3.Init.NSS = SPI_NSS_SOFT;
  hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi3.Init.CRCPolynomial = 10;
  if (HAL_SPI_Init(&hspi3) != HAL_OK)
  {
    Error_Handler();
  }

}

/* USART3 init function */
static void MX_USART3_UART_Init(void)
{

  huart3.Instance = USART3;
  huart3.Init.BaudRate = 460800;
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
  huart3.Init.StopBits = UART_STOPBITS_1;
  huart3.Init.Parity = UART_PARITY_NONE;
  huart3.Init.Mode = UART_MODE_TX_RX;
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart3) != HAL_OK)
  {
    Error_Handler();
  }

}

/** Configure pins as 
        * Analog 
        * Input 
        * Output
        * EVENT_OUT
        * EXTI
*/
static void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, ADS_CS_Pin|MPU_CS_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(PW_ENA_GPIO_Port, PW_ENA_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, SEN_FS1_Pin|LED_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin : ADS_DRDY_Pin */
  GPIO_InitStruct.Pin = ADS_DRDY_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  HAL_GPIO_Init(ADS_DRDY_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : ADS_CS_Pin MPU_CS_Pin */
  GPIO_InitStruct.Pin = ADS_CS_Pin|MPU_CS_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pin : PW_KEY_Pin */
  GPIO_InitStruct.Pin = PW_KEY_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(PW_KEY_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : PW_ENA_Pin */
  GPIO_InitStruct.Pin = PW_ENA_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(PW_ENA_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : SEN_FS1_Pin */
  GPIO_InitStruct.Pin = SEN_FS1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(SEN_FS1_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : SEN_DRDY_Pin */
  GPIO_InitStruct.Pin = SEN_DRDY_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(SEN_DRDY_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : LED_Pin */
  GPIO_InitStruct.Pin = LED_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI4_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI4_IRQn);

  HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);

}

/* USER CODE BEGIN 4 */

//======== USB =======
void USB_Print(unsigned char* s){
	uint8_t* ptr = s;
	int i = 0;
	while(*s++)
		i++;
	/*
	if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_9) == GPIO_PIN_SET )
		while( CDC_Transmit_FS(ptr,i) == USBD_BUSY);
	*/
	
	while( (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_9) == GPIO_PIN_SET) && (CDC_Transmit_FS(ptr,i) == USBD_BUSY));
}

void USB_SendBits(uint8_t b){
	int _i = 0;
	uint8_t buf[8];
	
	if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_9) == GPIO_PIN_SET )
		USB_Print("0b");
	
	for(_i = 7; _i >= 0; _i--){
		if( (b >> _i) & 0x01)
			buf[7-_i] = 49;
		else
			buf[7-_i] = 48;
	}
	
	while( (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_9) == GPIO_PIN_SET) && (CDC_Transmit_FS(buf,8) == USBD_BUSY));

}

void USB_SendNumber(int32_t x){
	uint8_t value[10]; //a temp array to hold results of conversion
  int i = 0, k = 0; //loop index
  
  do
  {
    value[i++] = (char)(x % 10) + '0'; //convert integer to character
		x /= 10;
  } while(x);
  
	for(k = i - 1; k >= 0; k--){
		while( (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_9) == GPIO_PIN_SET) && (CDC_Transmit_FS(&value[k],1) == USBD_BUSY));
	}
}


void USB_SendByte(uint8_t* fb){
	while( (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_9) == GPIO_PIN_SET) && (CDC_Transmit_FS(fb,sizeof(int8_t)) == USBD_BUSY));
}

void USB_Send2Byte(uint8_t* fb){
	while( (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_9) == GPIO_PIN_SET) && (CDC_Transmit_FS(fb,2*sizeof(int8_t)) == USBD_BUSY));
}
	
	
void USB_Send3Byte(uint8_t* fb){
	while( (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_9) == GPIO_PIN_SET) && (CDC_Transmit_FS(fb,3*sizeof(int8_t)) == USBD_BUSY));
}

void USB_Send4Byte(uint8_t* fb){
		while( (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_9) == GPIO_PIN_SET) && (CDC_Transmit_FS(fb,sizeof(int32_t)) == USBD_BUSY));
}

//======== SPI =======
uint8_t transferMPU(uint8_t send){
	uint8_t rx = 0x00;
	
	HAL_SPI_TransmitReceive(&hspi3, &send, &rx, sizeof(rx),0x1000);
	
	return rx;
}

uint8_t transferSPI(uint8_t send){
	uint8_t rx = 0x00;
	
	HAL_SPI_TransmitReceive(&hspi1, &send, &rx, sizeof(rx),0x1000);
	
	return rx;
}

//======== USART =======
void USART_Send(uint8_t* cmd){
	uint8_t* ptr = cmd;
	int size = 0;
	
	while(*cmd++)
		size++;
	
	//while( HAL_UART_Transmit(&huart3, ptr, size, 0x1000) != HAL_OK);
	HAL_UART_Transmit(&huart3, ptr, size, 0x1000);
	
	if(verbose){
		USB_Print("Cmd: ");
		USB_Print(ptr);
	}
}

void USART_Send2Byte(uint8_t* fb){
	//while( HAL_UART_Transmit(&huart3, fb, sizeof(int32_t), 0x1000) != HAL_OK);
	HAL_UART_Transmit(&huart3, fb, 2*sizeof(int8_t), 0x1000);
	USART_Send("\r\n");
}

void USART_Send4Byte(uint8_t* fb){
	//while( HAL_UART_Transmit(&huart3, fb, sizeof(int32_t), 0x1000) != HAL_OK);
	HAL_UART_Transmit(&huart3, fb, sizeof(int32_t), 0x1000);
	USART_Send("\r\n");
}

/* --------------- Inetrrupts ------------------------------------------*/
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
	if(GPIO_Pin == GPIO_PIN_13){
		intDRDY = true;
	}
	if(GPIO_Pin == PW_KEY_Pin){
		if(HAL_GPIO_ReadPin(PW_KEY_GPIO_Port, PW_KEY_Pin) == GPIO_PIN_SET)
			timerPowerDown = HAL_GetTick();
		if(HAL_GPIO_ReadPin(PW_KEY_GPIO_Port, PW_KEY_Pin) == GPIO_PIN_RESET){
			if( HAL_GetTick() - timerPowerDown > 1000)
				HAL_GPIO_WritePin(PW_ENA_GPIO_Port, PW_ENA_Pin, GPIO_PIN_RESET);
		}	
	}
		
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
	if(huart == &huart3){
		// If you use esp8266.c uncomment this
		//addRingBuffer(ansESP);
		//HAL_UART_Receive_IT(&huart3, &ansESP, 1);	
	}
}

void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart){
		
}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler */
  /* User can add his own implementation to report the HAL error return state */
  while(1) 
  {
  }
  /* USER CODE END Error_Handler */ 
}

#ifdef USE_FULL_ASSERT

/**
   * @brief Reports the name of the source file and the source line number
   * where the assert_param error has occurred.
   * @param file: pointer to the source file name
   * @param line: assert_param error line source number
   * @retval None
   */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */

}

#endif

/**
  * @}
  */ 

/**
  * @}
*/ 

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
